package uebung12.orienteering;

import uebung12.orienteering.exceptions.InvalidAreaException;
import uebung12.orienteering.exceptions.InvalidCoordinatesException;
import uebung12.orienteering.exceptions.InvalidStartpointException;
import uebung12.orienteering.exceptions.NotAWaypointException;
import acm.program.DialogProgram;

public class OrienteeringDialog extends DialogProgram {

	/**
   * 
   */
	private static final long serialVersionUID = -2064310234134016114L;

	/**
	 * Loads a map and interacts with the user
	 */
	public void run() {
		// TODO implement this...
		// creeate a map
		char[][] simpleArea = new char[][] { { '#', '~', '~', '0', '0', '#' },
				{ '0', '^', '^', '0', '^', '~' },
				{ '0', '0', '^', '0', '0', '0' },
				{ '0', '0', '#', '^', '#', '0' },
				{ '~', '~', '0', '^', '^', '0' },
				{ '0', '~', '0', '0', '0', '0' }, };
		// create an Orienteering object
		Orienteering orienteering;
		try {
			orienteering = new Orienteering(simpleArea);
		} catch (InvalidAreaException e) {
			println("Beim Initialisieren der Karte ist ein Fehler aufgetreten:\n"
					+ e.getMessage());
			return;
		}
		println(orienteering.toString());

		int x = 0, y = 0;
		// infinite loop:
		while (true) {
			try {
				// 1. ask for x coordinate; if -1, exit program
				x = Integer
						.parseInt(readLine("Bitte geben Sie die x-Koordinate der Startposition an. Zum Beenden: -1"));
				if (x == -1)
					break;
				// 2. ask for y coordinate; if -1, exit program
				y = Integer
						.parseInt(readLine("Bitte geben Sie die y-Koordinate der Startposition an. Zum Beenden: -1"));
				if (y == -1)
					break;
			} catch (NumberFormatException e) {
				println("Fehlerhafte Eingabe:\n" + e.getMessage() + "\nUm das Programm zu beenden geben Sie '-1' ein.");
				e.printStackTrace();
			}
			// 3. run "reachable"
			try {
				orienteering.reachable(x, y);
			} catch (InvalidStartpointException e) {
				println("Die angegebenen Koordinaten stellen keinen Startpunkt dar:\n"
						+ e.getMessage());
			}
			println(orienteering.toString());

		}
	}

	/**
	 * Main method. This is automatically called by the environment when your
	 * program starts.
	 * 
	 * @param args
	 *            command-line arguments (ignored here)
	 */
	public static void main(String[] args) {
		new OrienteeringDialog().start();
	}
}
